<extend name="Center/base"/>
<block name="main_content">
    <div class=" pd10 common_block_border">
        <div>
            <div class="left">
                <h2 class="common_block_title">购物车</h2>
            </div>

            <div class="clearfix"></div>
        </div>
        <form action="{:U('store/Center/pay')}" method="post">

            <div class="pd10">
                <ul>
                    <volist name="items" id="vo">
                        <li class="pd10 f12px clearfix item_line">
                            <div class="col-xs-3"><img style="width: 50px;height: 50px"
                                                       src="{$vo.good.cover_id|getThumbImageById=50,50}"></div>
                            <div class="col-xs-3"
                                 style="width: 200px;overflow: hidden;margin-bottom: 10px;padding: 5px;">
                                <a style="width: 100%" class="text-more"
                                   href="{:U('store/Index/info',array('info_id'=>$vo['goods_id']))}" target="_blank">{$vo.good.title}</a>
                                <br/>
                                <br/>
                                <a onclick="removeItem({$vo['goods_id']},this)">删除</a> <span class="cgrey">|</span>
                                {:W('FavBtn/show',array(array('info'=>$vo['good'])))}
                            </div>

                            <div class="col-xs-6 text-right" style="line-height: 28px;">
                                ￥ <span class="comput_cny"
                                        data="{$vo.good.price}">{$vo.good.price|number_format=2}</span>元
                                <b>X</b>
                                <input type="hidden" class="comput_count pd5 tcenter f14px" name="goods_id[]"
                                       value="{$vo.goods_id}">
                                <input type="number" style="width: 60px" name="count[]"
                                       class="comput_count pd5 tcenter f14px" data="{$vo['goods_id']}"
                                       value="{$vo.count}"> =
                                <span class="comput_row_total f14px"></span>
                            </div>

                        </li>
                    </volist>
                    <if condition="count($items) eq 0">
                        <div style="font-size: 32px;color: grey" class="text-center ">购物车内暂无任何商品。</div>
                    </if>
                </ul>
            </div>
            <div class="pd10 tright f16px">
                <span class="">总价：</span> ￥ <span class="comput_total_cny">0</span>
                <br/>
                <br/>
                <input class="c_btn_buy" type="submit" value="下单">
            </div>
        </form>
    </div>

    <style>
        .item_line:hover {
            background: #ebf6ff;
        }

        .item_line {
            border: dashed 1px #ccc;
            margin-top: -1px;
        }

    </style>
    <script>
        var total_cny = 0;
        $(function () {

            $('.comput_count').change(comput_event);
            $('.comput_count').each(comput_event);

        });


        comput_event = function comput() {
            if (isNaN($(this).val())) {
                $(this).val(1);
            } else {
                if ($(this).val() <= 0) {
                    $(this).val(1);
                }
            }
            var cny = parseFloat($(this).parent().parent().find('.comput_cny').attr('data'));
            var count = $(this).val();
            var good_id = $(this).attr('data');
            $.post(U('store/Center/_cart_set_item_count'), {good_id: good_id, count: count}, function (msg) {
            }, 'json');

            $(this).parent().parent().find('.comput_row_total').html('￥ <span class="comput_line_cny" data="' + (cny * count) + '">' + fmoney(cny * count, 2) + '</span>元 ');
            comput_all();
        };
        comput_total_cny = function () {
            total_cny += parseFloat($(this).attr('data'));
        };
        comput_all = function () {
            total_cny = 0;
            $('.comput_line_cny').each(comput_total_cny);
            $('.comput_total_cny').text(fmoney(total_cny, 2));
        };


        function fmoney(s, n) {
            n = n > 0 && n <= 20 ? n : 2;
            s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
            var l = s.split(".")[0].split("").reverse(),
                    r = s.split(".")[1];
            t = "";
            for (i = 0; i < l.length; i++) {
                t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
            }
            return t.split("").reverse().join("") + "." + r;
        }

        /**
         * 移除商品
         */
        function removeItem(goods_id, obj) {
            var ok = confirm('是否确定删除本商品？');
            $.post(U('store/Center/cartRemoveItem'), {id: goods_id}, function (msg) {


                if(msg.status==1){

                    toast.success(msg.info,L('_KINDLY_REMINDER_'));
                    setTimeout(function(){
                        window.location.reload();
                    },500);
                }else{
                    toast.error(msg.info,L('_KINDLY_REMINDER_'));
                }
            }, 'json');
        }
    </script>
</block>

